
package com.insigma.business.qrcode.merchant._provider.impl;

import com.google.common.cache.Cache;

import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.MemCacheManager;

import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.welfare.dto.CareMerchantStreetBO;
import com.insigma.business.qrcode.welfare.dto.CareWelfareMerchantInfoDTO;
import com.insigma.business.qrcode.welfare.dto.CareWelfareMerchantStreetDTO;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantInfoDO;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantStreetDO;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantStreetInsertRequest;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantStreetSearchRequest;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantStreetUpdateRequest;
import com.insigma.business.qrcode.merchant.service.CareWelfareMerchantInfoService;
import com.insigma.business.qrcode.merchant._provider.CareWelfareMerchantStreetProvider;
import com.insigma.business.qrcode.merchant.service.CareWelfareMerchantStreetService;
import com.insigma.business.qrcode.common.converter.CareWelfareMerchantInfoProviderConverter;
import com.insigma.business.qrcode.common.converter.CareWelfareMerchantStreetProviderConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;


@Slf4j
@Service
@Component("careWelfareMerchantStreetProvider")
public class CareWelfareMerchantStreetProviderImpl extends BaseProviderImpl implements CareWelfareMerchantStreetProvider {

    @Autowired
    private CareWelfareMerchantStreetService careWelfareMerchantStreetService;

    @Autowired
    private CareWelfareMerchantInfoService careWelfareMerchantInfoService;


    Cache<String, Object> localCache = MemCacheManager.getInstance(5, TimeUnit.MINUTES);


    @Override
    public BasePagingResponse<List<CareWelfareMerchantStreetDTO>> searchCareWelfareMerchantStreet(CareWelfareMerchantStreetSearchRequest careWelfareMerchantStreetSearchRequest) {
        return null;
    }

    @Override
    public CareMerchantStreetBO getCachedStreet(String merchantId) {
        String key = "STREET:" + merchantId;
        Object ifPresent = localCache.getIfPresent(key);
        if (ifPresent != null) {
            return (CareMerchantStreetBO)ifPresent;
        }
        CareMerchantStreetBO streetBO = new CareMerchantStreetBO();
        CareWelfareMerchantStreetDO byMerchantId = careWelfareMerchantStreetService.getByMerchantId(merchantId);
        if (byMerchantId != null) {
            streetBO.setId(byMerchantId.getId());
            streetBO.setName(byMerchantId.getName());
        }
        localCache.put(key, streetBO);
        return streetBO;
    }

    @Override
    public BaseResponse<CareWelfareMerchantStreetDTO> getCareWelfareMerchantStreet(BaseGetRequest baseGetRequest) {
        final BaseResponse<CareWelfareMerchantStreetDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CareWelfareMerchantStreet的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CareWelfareMerchantStreetDO careWelfareMerchantStreetDO = careWelfareMerchantStreetService.get((String)baseGetRequest.getId());
                final CareWelfareMerchantStreetDTO remoteObj = CareWelfareMerchantStreetProviderConverter.convertResponseDO2DTO(careWelfareMerchantStreetDO);
                if (remoteObj != null) {
                    List<CareWelfareMerchantInfoDO> careWelfareMerchantInfoDOS = careWelfareMerchantInfoService.listByStreetId(remoteObj.getId());
                    final List<CareWelfareMerchantInfoDTO> careWelfareMerchantInfoDTOList = CareWelfareMerchantInfoProviderConverter.convertResponseDOList2DTOList(careWelfareMerchantInfoDOS);
                    remoteObj.setMerchants(careWelfareMerchantInfoDTOList);
                }

                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_STREET_GET;
            }
        });
        return baseResponse;    }

    @Override
    public BaseResponse<String> insertCareWelfareMerchantStreet(CareWelfareMerchantStreetInsertRequest insertRequest) {
        BizCheckUtil.checkNotNull(insertRequest, "新增CareWelfareMerchantStreet的请求参数不能为空");

        final CareWelfareMerchantStreetDO careWelfareMerchantStreetDO = CareWelfareMerchantStreetProviderConverter.convertRequestIO2DO(insertRequest);
        careWelfareMerchantStreetDO.setPublished(false);
        careWelfareMerchantStreetService.insert(careWelfareMerchantStreetDO);

        return ResponseUtils.buildSuccessResponse(careWelfareMerchantStreetDO.getId());    }

    @Override
    public BaseResponse<Integer> updateCareWelfareMerchantStreet(CareWelfareMerchantStreetUpdateRequest updateRequest) {
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CareWelfareMerchantStreet的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");
            }
            @Override
            public void executeService() {
                CareWelfareMerchantStreetDO careWelfareMerchantStreetDO = CareWelfareMerchantStreetProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = careWelfareMerchantStreetService.update(careWelfareMerchantStreetDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_STREET_UPDATE;
            }
        });
        return baseResponse;    }

    @Override
    public BaseResponse<Integer> deleteCareWelfareMerchantStreet(BaseDeleteRequest deleteRequest) {
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CareWelfareMerchantStreet的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(),"操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getModifierId(),"操作人不能为空");
            }
            @Override
            public void executeService() {
                final Integer affectRowNum = careWelfareMerchantStreetService.delete((String)deleteRequest.getId(),deleteRequest.getModifierType(),(String)deleteRequest.getModifierId());

                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_STREET_DELETE_LOGICALLY;
            }
        });
        return baseResponse;    }
}
